package br.com.poliwar.model {
	import br.com.poliwar.utils.grafo.Aresta;
	import br.com.poliwar.utils.grafo.IVertice;
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Transform;
	import gs.TweenMax;
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class Territorio extends EventDispatcher implements IVertice{
		
		private var _pino:Pino;
		private var _visual:MovieClip;
		private var _exercitos:int;
		private var _nome:String;
		private var qtd:int;
		
		private var _index:int;
		
		private var _aresta:Aresta;
		
		private var _autoOcultarPino:Boolean;
		private var _habilitado:Boolean;
		
		public function Territorio(index:int, nome:String, visual:MovieClip, pino:Pino, exercitos:int = 0 ) {
			this._visual = visual;
			this._pino = pino;
			this.exercitos = exercitos;
			this._nome = nome;
			this.autoOcultarPino = false;
			this.habilitado = false;
			
			this._visual.addChild (this._pino);
			
			this._pino.x = this.visual.width / 2;
			this._pino.y = this.visual.height / 2;
			this._pino.mouseChildren = false;
			
			this._visual.mouseChildren = false;
			this._visual.mouseEnabled = true;
			
			this._visual.addEventListener (MouseEvent.MOUSE_OVER, onMouseOver);
			this._visual.addEventListener (MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onMouseOut(e:MouseEvent):void {
			if (this.habilitado) {
				this._visual.filters = [];
			}
			if (this.autoOcultarPino) {
				this.iniciarAutoOcultarPino ();
			}
		}
		
		private function onMouseOver(e:MouseEvent):void {
			if (this.habilitado) {
				this._visual.filters = [new GlowFilter (this.corPino,1,10,10,10)];
				this._visual.parent.setChildIndex (this._visual, this._visual.parent.numChildren - 1);
			}
			if (this.autoOcultarPino) {
				TweenMax.to (this._pino, 0.5, { alpha: 1, delay: 0, overwrite: 1 } );
			}
		}
		
		private function iniciarAutoOcultarPino ():void {
			TweenMax.to (this._pino, 0.5, { alpha: 0, delay: 2, overwrite: 1 } );
		}
		
		public function set corPino (cor:uint):void {
			var transf:Transform = this._pino.fundoCor.transform;
			var colorTransf:ColorTransform = transf.colorTransform;
			colorTransf.color = cor;
			transf.colorTransform = colorTransf;
			this._pino.fundoCor.transform = transf;
		}
		
		public function get corPino ():uint {
			return this._pino.fundoCor.transform.colorTransform.color;
		}
		
		public function adicionarExercitos (qtd:int):void {
			if (qtd < 0) qtd = 0;
			this._exercitos += qtd
			this._pino.campoQtd.text = this._exercitos.toString ();
		}
		
		public function removerExercitos (qtd:int):void {
			if (qtd < 0) qtd = 0;
			this._exercitos += qtd
			this._pino.campoQtd.text = this._exercitos.toString ();
		}
		
		/* INTERFACE br.com.poliwar.utils.grafo.IVertice */
		
		public function get index():int {
			return this._index;
		}
		
		public function get visual():MovieClip { return _visual; }
		
		public function set visual(value:MovieClip):void {
			_visual = value;
		}
		
		public function get exercitos():int { return _exercitos; }
		
		public function set exercitos(value:int):void {
			_exercitos = value;
			this._pino.campoQtd.text = this._exercitos.toString ();
		}
		
		public function get nome():String { return _nome; }
		
		public function set nome(value:String):void {
			_nome = value;
		}
		
		public function get aresta():Aresta { return _aresta; }
		
		public function set aresta(value:Aresta):void {
			_aresta = value;
		}
		
		public function get habilitado():Boolean { return _habilitado; }
		
		public function set habilitado(value:Boolean):void {
			_habilitado = value;
		}
		
		public function get autoOcultarPino():Boolean { return _autoOcultarPino; }
		
		public function set autoOcultarPino(value:Boolean):void {
			_autoOcultarPino = value;
			if (value == false) {
				this._pino.alpha = 1;
			} else {
				this.iniciarAutoOcultarPino ();
			}
		}
		
	}

}